package com.xj.community.Service;

import com.xj.community.dao.DiscussPostMapper;
import com.xj.community.entity.DiscussPost;
import com.xj.community.util.SensitiveWordsFilter;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class DiscussPostService {
    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private SensitiveWordsFilter wordsFilter;
    public List<DiscussPost> discussPostList(int userId, int offset, int limit){
        return discussPostMapper.discussPostList(userId,offset,limit);
    }

    public int selectAllDiscussPost(int userId){
        return discussPostMapper.selectAllDiscussPost(userId);
    }

    public int insertDiscussPost(DiscussPost discussPost){
        if (discussPost == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }

        // 转义HTML标记
        discussPost.setTitle(HtmlUtils.htmlEscape(discussPost.getTitle()));
        discussPost.setContent(HtmlUtils.htmlEscape(discussPost.getContent()));
        //过滤敏感词
        discussPost.setContent(wordsFilter.filter(discussPost.getContent()));
        discussPost.setTitle(wordsFilter.filter(discussPost.getTitle()));
        return discussPostMapper.insertDiscussPost(discussPost);
    }

    //根据id查询用户帖子
    public DiscussPost findDiscussPostById(int id){
        return discussPostMapper.findDiscussPostById(id);
    }

    //处理更新帖子评论数量的业务
    public int updateCount(int id,int count){
        return discussPostMapper.updateCount(id,count);
    }

}
